function [hist1,hist2,hist3] = Color_Features(rgb, mask, space, normalize)
%COLOR_FEATURES - Outputs three histograms for each color channel of the 
% input rgb image transformed into the specified color space

global numBins

if strcmp(space,'hsi')
    
    maxH = 2*pi; maxS = 1; maxI = 255;
    incH = maxH/numBins; incS = maxS/numBins; incI = maxI/numBins;
    
    hsi = rgb2hsi(rgb);
    h = hsi(:,:,1);
    s = hsi(:,:,2);
    i = hsi(:,:,3);
    
    H = h(mask); H = H(H~=-1);
    S = s(mask);
    I = i(mask);
    
    histH = histc(H, incH/2:incH:maxH-incH/2);
    histS = histc(S, incS/2:incS:maxS-incS/2);
    histI = histc(I, incI/2:incI:maxI-incI/2);
    
    if normalize
        histH = histH/sum(histH);
        histS = histS/sum(histS);
        histI = histI/sum(histI);
    end
    
    hist1 = histH';
    hist2 = histS'; 
    hist3 = histI';
    
elseif strcmp(space,'hsv')
    
    maxH = 1; maxS = 1; maxV = 1;
    incH = maxH/numBins; incS = maxS/numBins; incV = maxV/numBins;   
    
    hsv = rgb2hsv(rgb);
    h = hsv(:,:,1); 
    s = hsv(:,:,2); 
    v = hsv(:,:,3);
    
    H = h(mask);
    S = s(mask);
    V = v(mask);
    
    histH = histc(H, incH/2:incH:maxH-incH/2);
    histS = histc(S, incS/2:incS:maxS-incS/2);
    histV = histc(V, incV/2:incV:maxV-incV/2);
    
    if normalize
        histH = histH/sum(histH);
        histS = histS/sum(histS);
        histV = histV/sum(histV);
    end
    
    hist1 = histH';
    hist2 = histS';
    hist3 = histV';
   
elseif strcmp(space,'rgb')
    
    maxR = 255; maxG = 255; maxB = 255;
    incR = maxR/numBins; incG = maxG/numBins; incB = maxB/numBins;   
    
    r = rgb(:,:,1); 
    g = rgb(:,:,2); 
    b = rgb(:,:,3);
    
    R = r(mask);
    G = g(mask);
    B = b(mask);
    
    histR = histc(R, incR/2:incR:maxR-incR/2);
    histG = histc(G, incG/2:incG:maxG-incG/2);
    histB = histc(B, incB/2:incB:maxB-incB/2);
    
    if normalize
        histR = histR/sum(histR);
        histG = histG/sum(histG);
        histB = histB/sum(histB);
    end
    
    hist1 = histR';
    hist2 = histG';
    hist3 = histB';
end




end

